<?php

require_once 'db.php';

/**
 * Clase para paginar registros
 * @author Anyul Rivas
 */
class paginacion extends db {

    public $maxRows;
    public $query;
    public $registros;
    public $totalRows;
    public $currentPage;
    public $localhost;
    private $debug;
    private $registrosTotales;
    private $queryString;
    private $totalPages;

    function __construct($debug=false) {
        set_error_handler("Misc::error_handler");
        $this->debug=$debug;
    }

    /**
     * Clase principal para el paginado
     */
    public function paginar($query, $MaxRows=10) {
        $this->maxRows = $MaxRows;
        $this->query = $query;
        if ($this->query != "") {
            $this->currentPage = $_SERVER["PHP_SELF"];
            $this->pageNum = 0;
            // <editor-fold defaultstate="collapsed" desc="page_num">
            if (isset($_GET['pageNum'])) {
                $this->pageNum = $_GET['pageNum'];
            }
            // </editor-fold>

            $this->startRow = $this->pageNum * $this->maxRows;

            $this->limit = sprintf("%s LIMIT %d, %d", $this->query, $this->startRow, $this->maxRows);

            $this->registros_limit = $this->dame_query($this->limit);
            if ($this->registros_limit['suceed'] && count($this->registros_limit) > 0) {
                // <editor-fold defaultstate="collapsed" desc="debug">
                if ($this->debug) {
                    krumo::dump($this->registros_limit);
                }
                // </editor-fold>

                $this->registros = $this->registros_limit['data'];
                if (isset($_GET['totalRows'])) {
                    $this->totalRows = $_GET['totalRows'];
                } else {
                    $this->registrosTotales = $this->dame_query($this->query);
                    // <editor-fold defaultstate="collapsed" desc="debug">
                    if ($this->debug) {
                        krumo::dump($this->registrosTotales);
                    }
                    // </editor-fold>
                    // <editor-fold defaultstate="collapsed" desc="comprobando registros">
                    if (isset($this->registrosTotales['suceed']) &&
                            isset($this->registrosTotales['stats']['affected_rows']) &&
                            count($this->registrosTotales['stats']['affected_rows']) > 0) {
                        $this->totalRows = $this->registrosTotales['stats']['affected_rows'];
                    }
                    // </editor-fold>
                }
                $this->totalPages = ceil($this->totalRows / $this->maxRows) - 1;

                $this->queryString = "";
                // <editor-fold defaultstate="collapsed" desc="query_string">
                if (!empty($_SERVER['QUERY_STRING'])) {
                    $this->params = explode("&", $_SERVER['QUERY_STRING']);
                    $newParams = array();
                    foreach ($this->params as $param) {
                        if (stristr($param, "pageNum") == false && stristr($param, "totalRows") == false) {
                            array_push($newParams, $param);
                        }
                    }
                    if (count($newParams) != 0) {
                        $this->queryString = "&" . htmlentities(implode("&", $newParams));
                    }
                }
                // </editor-fold>

                $this->queryString = sprintf("&totalRows=%d%s", $this->totalRows, $this->queryString);
            } else {
                $this->registros = 0;
            }
        } else {
            echo "<h1>El query está vacío,</h1>";
        }
    }

    public function mostrar_paginado($cellpadding=0, $cellspacing=0, $width=100, $border=0) {
        if (isset($this->queryString)) {
            // <editor-fold defaultstate="collapsed" desc="Links">
            $primero = " <a href='" . $this->currentPage . "?pageNum=0" . $this->queryString . "'>&laquo; Primero </a>";
            $anterior = " <a href='" . $this->currentPage . "?pageNum=" . max(0, $this->pageNum - 1) . $this->queryString . "'>&lsaquo; Anterior </a> ";
            $siguiente = " <a href='" . $this->currentPage . "?pageNum=" . min($this->totalPages, $this->pageNum + 1) . $this->queryString . "'> Siguiente &rsaquo;</a> ";
            $ultimo = " <a href='" . $this->currentPage . "?pageNum=" . $this->totalPages . $this->queryString . "'>&Uacute;ltimo &raquo;</a>";
            // </editor-fold>
            // <editor-fold defaultstate="collapsed" desc="tabla">
            $html = "<table class='full' border='" . $border . "' cellpadding='" . $cellpadding . "' cellspacing='" . $cellspacing . "' width='" . $width . "%'>

    <tr>
        <td>";
            if ($this->pageNum > 0) {
                $html.= $primero;
            }
            $html.="</td>
        <td>";
            if ($this->pageNum > 0) {
                $html.=$anterior;
            }
            $html.="</td>
        <td>";
            if ($this->pageNum < $this->totalPages) {
                $html.= $siguiente;
            }
            $html.="</td>
        <td>";
            if ($this->pageNum < $this->totalPages) {
                $html.= $ultimo;
            }
            $html.="</td>
    </tr>
</table>";
            // </editor-fold>
            if ($this->debug) {
                krumo::dump($html);
            }
            if (count($this->registros) > 0) {
                echo $html;
            }
        }
    }

    public function __destruct() {
        restore_error_handler();
    }

}
